package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(value = "SPU")
@RequestMapping("/admin/product")
@RestController
public class BaseSpuController {

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SpuPosterService spuPosterService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    @Autowired
    BaseCategory3Service baseCategory3Service;

    @Autowired
    BaseSaleAttrService baseSaleAttrService;

    @Autowired
    BaseTrademarkService baseTrademarkService;

    @Autowired
    FileUploadService fileUploadService;


    /**
     *获取spu分页列表
     * @param page 第几页
     * @param limit 每页数
     * @param category3Id 三级分类id
      * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result getPage(@PathVariable("page")Long page,
                          @PathVariable("limit")Long limit,
                          @RequestParam("category3Id")Long category3Id){
        Page<SpuInfo> records = spuInfoService.page(new Page<>(page, limit),
                new QueryWrapper<SpuInfo>().eq("category3_id", category3Id));
        return Result.ok(records);
    }

    /**
     * 获取销售属性
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        List<BaseSaleAttr> list = baseSaleAttrService.list();
        return Result.ok(list);
    }

    /**
     * 获取品牌属性
     */
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getBaseTrademark(){
        List<BaseTrademark> list = baseTrademarkService.list();
        return Result.ok(list);
    }


    /**
     * 添加spu
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result postSaveSpuInfo(@RequestBody SpuInfo spuInfo){
        spuInfoService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/fileUpload")
    public Result postfileUpload(@RequestPart("file") MultipartFile file){

        //1、把文件存到对象存储（Minio）
       String url = fileUploadService.upload(file);
        //2、把文件存好的访问地址返回给前端
        return Result.ok(url);
    }


}
